<?php
	/** SNS facebook類
	*	Date: 	2012-5-17
	*	Author:	merlin
	*/
	class SNS_FB {
		
		private $_sdk;

		public function __construct(Facebook $_sdk) {
			$this->_sdk = $_sdk;
		}
		
		/** 獲取用戶信息 */
		public function getUserInfo() {
			$user_info = null;
			$user = $this->_sdk->getUser();
			if ($user) {
				try {
					$user_info = $this->_sdk->api('me');
					$user_info['avatar'] = array(
						'square' => 'http://graph.facebook.com/' . $user . '/picture?type=square',
						'small' => 'http://graph.facebook.com/' . $user . '/picture?type=small',
						'normal' => 'http://graph.facebook.com/' . $user . '/picture?type=normal',
						'large' => 'http://graph.facebook.com/' . $user . '/picture?type=large'
					);
				} catch (FacebookApiException $e) {
					$user = null;
				}
			}
			return $user_info;
		}
		
		/** 獲取用戶朋友列表 */
		public function getFriends() {
			$friends = null;
			try {
				$friends = $this->_sdk->api('me/friends');
			} catch (FacebookApiException $e) {}
			return $friends;
		}
		
		/** 獲取用戶相冊信息 */
		public function getAlbums() {
			$albums = null;
			try {
				$albums = $this->_sdk->api('me/albums');
			} catch(FacebookApiException $e) {}
			return $albums;
		}

		/** 新建相冊
		* @param	int		$album_id		相冊id
		* @params	array(
						'name'          =>$name,			//相冊名
						'description'   =>$description,		//相冊說明
						'location'   =>$location		    //相冊地點
					)
		* @return	array
		*/
		public function createAlbum(array $params) {
			$album = null;
			try {
				$album = $this->_sdk->api('me/albums', 'post', $params);
			} catch(FacebookApiException $e) {}
			return $album;
		}

		/** 獲取用戶相冊圖片
		* @param	int		$album_id		相冊id
		* @return	Array
		*/
		public function getPhotos($album_id){
			$photos = null;
			try {
				$photos = $this->_sdk->api($album_id . '/photos');
			} catch(FacebookApiException $e) {
			
			}
			return $photos;
		}

		/** 上傳圖片到用戶相冊
		* @param	int		$albumid		相冊id
		* @param	array(
						'source'    => $source,		//圖片
						'message'   => $message		//圖片說明
					)
		* @return	Array
		*/
		public function uploadPhoto($album_id, array $params) {
			$photo = null;
			try {
				$photo = $this->_sdk->api($album_id . '/photos', 'post', $params);
			} catch(FacebookApiException $e) {
			
			}
			return $photo;
		}

		/** 獲取用戶likes
		* @return	Array
		*/
		public function getLikes(){
			$likes = null;
			try {
				$likes = $this->_sdk->api('me/likes');
			} catch(FacebookApiException $e) {}
			return $likes;
		}

		/** 判斷是否like
		* @param	int		$object_id
		* @return	boolean
		*/
		public function isLike($object_id){
			$is_like = false;
			try{
				$like = $this->_sdk->api('me/likes/' . $object_id);
				$is_like = isset($like['data']) && !empty($like['data']);
			} catch(FacebookApiException $e) {}
			return $is_like;
		}

		/** post to wall
		* @params	array(
					'name'         => "name",
					'link'         => "http://link.hk/",
					'description'  => "description",
					'picture'      => "http://picture.jpg"
				);
		* @return	int
		*/
		public function postToWall(array $params){
			$feed_id = null;
			try{
				$feed_id = $this->_sdk->api('me/feed', 'post', $params);
			} catch(FacebookApiException $e) {print_r($e);}
			return $feed_id;
		}

		/** 獲取用戶在App中的權限
		* @return	Array
		*/
		public function getPermissions(){
			$permissions = null;
			try {
				$permissions = $this->_sdk->api('me/permissions');
				if(isset($permissions['data'][0])){
					$permissions = $permissions['data'][0];
				}else{
					$permissions = array();
				}
			} catch(FacebookApiException $e) {}
			return $permissions;
		}

	}

?>